import { create } from 'zustand'
import Variables from '@/common/Variables'

type State = {
    status: string,
    preDelay: number
}

type Action = {
    run: () => void
}

const sleep = async (delay = 0) => {
    return await new Promise((r) => setTimeout(r, delay));
};

// 创建 store
const useStore = create<State & Action>((set, get) => ({
    status: 'idle',
    preDelay: 100,
    postDelay: 100,
    maxRetries: 3,
    commonds: [],
    index: 0,
    pendingResolve: null,
    variables: new Variables(), // 变量
    start: async () => {
        set({
            status: 'playing',
        });
        get().run();
    },
    run: async () => {
        await sleep(get().preDelay)
    },


}))

const state = useStore.getState()


const play = async () => {

}

export default useStore

// 数据单一来源原则
// 变量
// store中的状态
// 

const queues = [
    {
        cmd: 'setVal',
        target: 'a',
        targetOptions: [],
        value: 'b',
        extra: {
            preDelay: 100,
            postDelay: 100,
        }
    },
    {
        cmd: 'getVal',
        target: 'a',
        targetOptions: [],
        value: '',
        extra: {
            preDelay: 100,
            postDelay: 100,
        }
    },
    {
        cmd: 'echo',
        target: '变量a的值为',
        targetOptions: [],
        value: '$a',
        extra: {
            preDelay: 100,
            postDelay: 100,
        }
    },
    {
        cmd: 'click',
        target: '//body',
        targetOptions: ['//body', 'body'],
        value: '',
        extra: {
            preDelay: 100,
            postDelay: 100,
        }
    },

]

